package com.security.lambda.predicate;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * Predicate -> 需要表示一个涉及类型T的布尔表达式时，使用
 *
 * @author roger
 * @email 190642964@qq.com
 * @create 2017-11-09 9:12
 **/
public class Main {

    /**
     * 过滤集合
     *
     * @param list 需要过滤的集合
     * @param p    过滤条件
     * @param <T>  类型
     * @return 过滤后的集合
     */
    public static <T> List<T> filter(List<T> list, Predicate<T> p) {
        List<T> results = new ArrayList<>();
        for (T s : list) {
            if (p.test(s)) {
                results.add(s);
            }
        }
        return results;
    }

    public static void main(String[] args) {

        Predicate<PredicateEntity> emptyPredicate = (PredicateEntity pe) -> pe.getWeight() > 10;

        List<PredicateEntity> list = new ArrayList<>();
        list.add(new PredicateEntity("荔枝", 2));
        list.add(new PredicateEntity("苹果", 5));
        list.add(new PredicateEntity("榴莲", 12));
        list.add(new PredicateEntity("西瓜", 15));

        List<PredicateEntity> notEmpty = filter(list, emptyPredicate);

        System.out.println(notEmpty.size());

        notEmpty.stream().forEach(predicateEntity -> System.out.println(predicateEntity));
    }
}
